﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QSettings>
#include <QTranslator>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_translator = new QTranslator(this);
    m_settingsFileName = qApp->applicationDirPath() + "/settings.ini";
    initUI();
    initSettings();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUI()
{
    ani_leftMenuContainer = new QPropertyAnimation(ui->leftMenuContainer,"maximumWidth");
    ani_centerMenuContainer = new QPropertyAnimation(ui->centerMenuContainer,"maximumWidth");
    ani_rightMenuContainer = new QPropertyAnimation(ui->rightMenuContainer,"maximumWidth");
    ani_popupNotificationContainer = new QPropertyAnimation(ui->popupNotificationContainer,"maximumHeight");

    this->setWindowFlags(Qt::FramelessWindowHint);

    ui->leftMenuContainer->setMaximumWidth(40);
    ui->centerMenuContainer->setMaximumWidth(0);
    ui->rightMenuContainer->setMaximumWidth(0);
    ui->popupNotificationContainer->setMaximumHeight(0);

    ui->homeBtn->setCheckable(true);
    ui->dataBtn->setCheckable(true);
    ui->reportBtn->setCheckable(true);
    ui->homeBtn->setChecked(true);
    ui->mainBodyPage->setCurrentIndex(0);

    ui->settingsBtn->setCheckable(true);
    ui->infoBtn->setCheckable(true);
    ui->helpBtn->setCheckable(true);
    ui->centerMenuPage->setCurrentIndex(0);
}

void MainWindow::initSettings()
{
    qDebug() << "settingsFileName:" << m_settingsFileName;
    QSettings settings(m_settingsFileName,QSettings::IniFormat);
    QString language = settings.value("General/language","zh").toString();
    if(language == "en")
    {
        ui->languageComb->setCurrentIndex(1);
    }else{
        ui->languageComb->setCurrentIndex(0);
    }
    changeLanguage(language);
}

void MainWindow::changeLanguage(QString targetLanguage)
{
    if(targetLanguage == "en")
    {
        m_translator->load(":/language/en_tr.qm");
    }
    else
    {
        m_translator->load(":/language/zh_tr.qm");
    }
    QApplication::installTranslator(m_translator);
    ui->retranslateUi(this);

    QSettings settings(m_settingsFileName,QSettings::IniFormat);
    settings.setValue("General/language",targetLanguage);
}

void MainWindow::playLeftMenuContainerAnimation()
{
    if(ui->leftMenuContainer->maximumWidth() > 50)
    {
        ani_leftMenuContainer->setStartValue(ui->leftMenuContainer->width());
        ani_leftMenuContainer->setEndValue(40);
        ani_leftMenuContainer->setDuration(200);
        ani_leftMenuContainer->start();
    }else
    {
        ani_leftMenuContainer->setStartValue(ui->leftMenuContainer->width());
        ani_leftMenuContainer->setEndValue(180);
        ani_leftMenuContainer->setDuration(200);
        ani_leftMenuContainer->start();
    }
}

void MainWindow::playCenterMenuContainerAnimation()
{
    if(ui->centerMenuContainer->maximumWidth() > 50)
    {
        ani_centerMenuContainer->setStartValue(ui->centerMenuContainer->width());
        ani_centerMenuContainer->setEndValue(0);
        ani_centerMenuContainer->setDuration(200);
        ani_centerMenuContainer->start();
    }else
    {
        ani_centerMenuContainer->setStartValue(ui->centerMenuContainer->width());
        ani_centerMenuContainer->setEndValue(250);
        ani_centerMenuContainer->setDuration(200);
        ani_centerMenuContainer->start();
    }
}

void MainWindow::playRightMenuContainerAnimation()
{
    if(ui->rightMenuContainer->maximumWidth() > 50)
    {
        ani_rightMenuContainer->setStartValue(ui->rightMenuContainer->width());
        ani_rightMenuContainer->setEndValue(0);
        ani_rightMenuContainer->setDuration(200);
        ani_rightMenuContainer->start();
    }else
    {
        ani_rightMenuContainer->setStartValue(ui->rightMenuContainer->width());
        ani_rightMenuContainer->setEndValue(250);
        ani_rightMenuContainer->setDuration(200);
        ani_rightMenuContainer->start();
    }
}

void MainWindow::playPopupNotificationContainerAnimation()
{
    if(ui->popupNotificationContainer->maximumHeight() > 50)
    {
        ani_popupNotificationContainer->setStartValue(ui->popupNotificationContainer->height());
        ani_popupNotificationContainer->setEndValue(0);
        ani_popupNotificationContainer->setDuration(200);
        ani_popupNotificationContainer->start();
    }else
    {
        ani_popupNotificationContainer->setStartValue(ui->popupNotificationContainer->height());
        ani_popupNotificationContainer->setEndValue(250);
        ani_popupNotificationContainer->setDuration(200);
        ani_popupNotificationContainer->start();
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = true;
        dragPos = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (isDragging) {
        move(event->globalPos() - dragPos);
        event->accept();
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = false;
        event->accept();
    }
}

void MainWindow::on_menuBtn_clicked()
{
    playLeftMenuContainerAnimation();
    if(ui->leftMenuContainer->width() > 50)
    {
        ui->menuBtn->setIcon(QIcon(":/icons/icons/align-justify.svg"));
    }else
    {
        ui->menuBtn->setIcon(QIcon(":/icons/icons/chevron-left.svg"));
    }
}

void MainWindow::on_closeBtn_clicked()
{
    this->close();
}


void MainWindow::on_minimizeBtn_clicked()
{
    this->showMinimized();
}


void MainWindow::on_restoreBtn_clicked()
{
    if(this->isMaximized())
    {
        this->showNormal();
        ui->restoreBtn->setIcon(QIcon(":/icons/icons/square.svg"));
    }else
    {
        this->showMaximized();
        ui->restoreBtn->setIcon(QIcon(":/icons/icons/copy.svg"));
    }
}


void MainWindow::on_profileBtn_clicked()
{
    if(ui->rightMenuContainer->width() == 0)
    {
        playRightMenuContainerAnimation();
    }
    ui->rightMenuPage->setCurrentIndex(0);
}

void MainWindow::on_moreBtn_clicked()
{
    if(ui->rightMenuContainer->width() == 0)
    {
        playRightMenuContainerAnimation();
    }
    ui->rightMenuPage->setCurrentIndex(1);
}

void MainWindow::on_notificationBtn_clicked()
{
    if(ui->popupNotificationContainer->height() == 0)
    {
        playPopupNotificationContainerAnimation();
    }
}

void MainWindow::on_colseRightMenuBtn_clicked()
{
    playRightMenuContainerAnimation();
}

void MainWindow::on_homeBtn_clicked()
{
    ui->mainBodyPage->setCurrentIndex(0);
    ui->homeBtn->setChecked(true);
    ui->dataBtn->setChecked(false);
    ui->reportBtn->setChecked(false);
}


void MainWindow::on_dataBtn_clicked()
{
    ui->mainBodyPage->setCurrentIndex(1);
    ui->homeBtn->setChecked(false);
    ui->dataBtn->setChecked(true);
    ui->reportBtn->setChecked(false);
}


void MainWindow::on_reportBtn_clicked()
{
    ui->mainBodyPage->setCurrentIndex(2);
    ui->homeBtn->setChecked(false);
    ui->dataBtn->setChecked(false);
    ui->reportBtn->setChecked(true);
}


void MainWindow::on_settingsBtn_clicked()
{
    if(ui->centerMenuContainer->width() == 0)
    {
        playCenterMenuContainerAnimation();
    }
    ui->centerMenuPage->setCurrentIndex(0);
    ui->settingsBtn->setChecked(true);
    ui->infoBtn->setChecked(false);
    ui->helpBtn->setChecked(false);
}


void MainWindow::on_infoBtn_clicked()
{
    if(ui->centerMenuContainer->width() == 0)
    {
        playCenterMenuContainerAnimation();
    }
    ui->centerMenuPage->setCurrentIndex(1);
    ui->settingsBtn->setChecked(false);
    ui->infoBtn->setChecked(true);
    ui->helpBtn->setChecked(false);
}


void MainWindow::on_helpBtn_clicked()
{
    if(ui->centerMenuContainer->width() == 0)
    {
        playCenterMenuContainerAnimation();
    }
    ui->centerMenuPage->setCurrentIndex(2);
    ui->settingsBtn->setChecked(false);
    ui->infoBtn->setChecked(false);
    ui->helpBtn->setChecked(true);
}


void MainWindow::on_closeMoreMenuBtn_clicked()
{
    playCenterMenuContainerAnimation();
    ui->settingsBtn->setChecked(false);
    ui->infoBtn->setChecked(false);
    ui->helpBtn->setChecked(false);
}


void MainWindow::on_closeNotificationBtn_clicked()
{
    playPopupNotificationContainerAnimation();
}


void MainWindow::on_languageComb_currentIndexChanged(int index)
{
    if(ui->centerMenuPage->width() < 10) return;
    QString language;
    if(ui->languageComb->currentIndex() == 1)
    {
        language = "en";
    }else
    {
        language = "zh";
    }
    changeLanguage(language);
}

